package sing.core.ext.util

import java.text.SimpleDateFormat
import java.util.*


// 时间戳转化为指定格式
fun Date.toFormat(format:String="yyyy-MM-dd HH:mm:ss"): String {
    val sdf = SimpleDateFormat(format)
    return sdf.format(this)
}

// 时间戳转化为指定格式
fun Date.toFormatS(format:String="yyyy-MM-dd HH:mm:ss:SSS"): String {
    val sdf = SimpleDateFormat(format)
    return sdf.format(this)
}

// 时间戳转化为指定格式
fun Date.getTodayEnWeek(): String {
    val calendar = Calendar.getInstance()
    val day = calendar[Calendar.DAY_OF_WEEK]
    var today: String? = null
    when (day) {
        1 ->  today = "Sunday"
        2 ->  today = "Monday"
        3 ->  today = "Tuesday"
        4 ->  today = "Wednesday"
        5 ->  today = "Thursday"
        6 ->  today = "Friday"
        7 ->  today = "Saturday"
    }
    return  "$today"
}
fun Date.getTodayCnWeek(): String {
    val calendar = Calendar.getInstance()
    calendar.time = this
    val day = calendar[Calendar.DAY_OF_WEEK]
    var today: String? = null
    when (day) {
        1 ->  today = "周日"
        2 ->  today = "周一"
        3 ->  today = "周二"
        4 ->  today = "周三"
        5 ->  today = "周四"
        6 ->  today = "周五"
        7 ->  today = "周六"
    }
    return  "$today"
}

// 聊天的格式，今天的是HH:mm，昨天就是"昨天"，
fun Date.getChatString(): String {
    return when {
        isToday() -> toFormat( "HH:mm")
        isYesterday() -> "昨天"
        else -> toFormat("yyyy/MM/dd")
    }
}

// 时间显示规则：一分钟之内的显示【刚刚】、一小时内的显示【1-59分钟前】、一天内的显示【1-23小时前】，昨天的显示【昨天】，之前的日期显示【09/01】，跨年的显示【2019/09/01】
fun Date.getListDate(): String {
    if (this.isInOneMinute()){
        return "刚刚"
    } else if (this.isYesterday()){
        return "昨天"
    } else if (this.isIn24Hour()){
        return format(this) // 1-59分钟前，1-23小时前
    } else if (this.isThisYear()){
        return toFormat("MM/dd")
    }else {
        return toFormat("yyyy/MM/dd")
    }
}

// 详情时间显示规则：今天之内的显示【19:57】，昨天显示【昨天19:57】，之前日期显示【09/01  19:57】，跨年的显示【2019/09/01   19:57】
fun Date.getDetailDate(): String {
    return when {
        this.isToday() -> toFormat("HH:mm")
        this.isYesterday() -> "昨天${toFormat("HH:mm")}"
        this.isThisYear() -> toFormat("MM/dd HH:mm")
        else -> this.toFormat( "yyyy/MM/dd HH:mm")
    }
}

fun Date.isYesterday(): Boolean {
    val pre = Calendar.getInstance()
    val predate = Date(System.currentTimeMillis())
    pre.time = predate

    val cal = Calendar.getInstance()
    cal.time = this

    if (cal[Calendar.YEAR] == pre[Calendar.YEAR]) {
        val diffDay = (cal[Calendar.DAY_OF_YEAR] - pre[Calendar.DAY_OF_YEAR])
        if (diffDay == -1) {
            return true
        }
    }
    return false
}

//判断选择的日期是否是今天
fun Date.isToday(): Boolean {
    return isThisTime(this, "yyyy-MM-dd")
}

//判断选择的日期是否是明天
fun Date.isTomorrow(): Boolean {
    val pre = Calendar.getInstance()
    val predate = Date(System.currentTimeMillis())
    pre.time = predate

    val cal = Calendar.getInstance()
    cal.time = this

    if (cal[Calendar.YEAR] == pre[Calendar.YEAR]) {
        val diffDay = (cal[Calendar.DAY_OF_YEAR] - pre[Calendar.DAY_OF_YEAR])
        if (diffDay == 1) {
            return true
        }
    }
    return false
}

//判断选择的日期是否是本月
fun Date.isThisMonth(): Boolean {
    return isThisTime(this, "yyyy-MM")
}

//判断选择的日期是否是本年
fun Date.isThisYear(): Boolean {
    return isThisTime(this, "yyyy")
}

// 是否是一分钟之内
fun Date.isInOneMinute():Boolean{
    val thisTime = System.currentTimeMillis() - this.time
    return thisTime <= 60 * 1000
}

// 判断是否超过另一个时间5分钟
fun Date.isInFiveMinute(other:Long):Boolean{
    val thisTime = this.time - other // 过去的时间小
    return thisTime <= 60 * 1000 * 5
}

// 是否是一小时之内
fun Date.isInOneHour():Boolean{
    val thisTime = System.currentTimeMillis() - this.time
    return thisTime <= 60 * 1000 * 60
}

// 是否是24小时之内
fun Date.isIn24Hour():Boolean{
    val thisTime = System.currentTimeMillis() - this.time
    return thisTime <= 60 * 1000 * 60 * 24
}

private fun isThisTime(time: Date, pattern: String): Boolean {
    val sdf = SimpleDateFormat(pattern)
    val param = sdf.format(time) //参数时间
    val now = sdf.format(Date()) //当前时间
    return if (param == now) {
        true
    } else false
}

fun format(date: Date): String {
    val ONE_MINUTE = 60000L
    val ONE_HOUR = 3600000L
    val ONE_DAY = 86400000L
    val ONE_WEEK = 604800000L

    val ONE_SECOND_AGO = "秒前"
    val ONE_MINUTE_AGO = "分钟前"
    val ONE_HOUR_AGO = "小时前"
    val ONE_DAY_AGO = "天前"
    val ONE_MONTH_AGO = "月前"
    val ONE_YEAR_AGO = "年前"

    val delta = Date().time - date.time
    if (delta < 1L * ONE_MINUTE) {
        val seconds: Long = toSeconds(delta)
        return "${(if (seconds <= 0) 1 else seconds)}$ONE_SECOND_AGO"
    }
    if (delta < 45L * ONE_MINUTE) {
        val minutes: Long = toMinutes(delta)
        return "${(if (minutes <= 0) 1 else minutes)}$ONE_MINUTE_AGO"
    }
    if (delta < 24L * ONE_HOUR) {
        val hours: Long = toHours(delta)
        return "${(if (hours <= 0) 1 else hours)}$ONE_HOUR_AGO"
    }
    if (delta < 48L * ONE_HOUR) {
        return "昨天"
    }
    if (delta < 30L * ONE_DAY) {
        val days: Long = toDays(delta)
        return "${(if (days <= 0) 1 else days)}$ONE_DAY_AGO"
    }
    return if (delta < 12L * 4L * ONE_WEEK) {
        val months: Long = toMonths(delta)
        "${(if (months <= 0) 1 else months)}$ONE_MONTH_AGO"
    } else {
        val years: Long = toYears(delta)
        "${(if (years <= 0) 1 else years)}$ONE_YEAR_AGO"
    }
}

private fun toSeconds(date: Long): Long {
    return date / 1000L
}

private fun toMinutes(date: Long): Long {
    return toSeconds(date) / 60L
}

private fun toHours(date: Long): Long {
    return toMinutes(date) / 60L
}

private fun toDays(date: Long): Long {
    return toHours(date) / 24L
}

private fun toMonths(date: Long): Long {
    return toDays(date) / 30L
}

private fun toYears(date: Long): Long {
    return toMonths(date) / 365L
}

/**
 * 是否在7天之内
 */
fun Date.isIn7Days(): Boolean {
    val calendar = Calendar.getInstance() //得到日历
    calendar.time = Date() //把当前时间赋给日历
    calendar.add(Calendar.DAY_OF_MONTH, -7) //设置为7天前
    val before7days = calendar.time //得到7天前的时间
    return before7days.time < this.time
}

//获取当前时间前后几天的时间
fun Date.beforeAfterDate(days: Int, format: String = "yyyy-MM-dd"): String {
    val nowTime = System.currentTimeMillis()
    val changeTimes = days * 24L * 60 * 60 * 1000
    return getStrTime((this.time + changeTimes).toString(), format)
}

fun getStrTime(timeStamp: String, format: String): String {
    val sdf = SimpleDateFormat(format)
    val l = java.lang.Long.valueOf(timeStamp)
    return sdf.format(Date(l)) //单位秒
}

// 转指定格式的字符串转化为指定格式
fun getStrTime(timeStamp: String, oldFormat: String , format: String): String {
    val sdf = SimpleDateFormat(oldFormat).parse(timeStamp) // 将原来的字符串转化为日期
    return sdf.toFormat(format) //单位秒
}

